package com.cwt_taxi_xa.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.cwt_taxi_xa.MainApplication;
import com.cwt_taxi_xa.constants.SPConstants;

public class SPUtils {

    /**
     * 保存在手机里面的文件名
     */
    private static final String FILE_NAME = "cwt_taxi_xa_spdata";


    /**
     * 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法
     * @param key
     * @param object
     */
    public synchronized static void setParam(String key, Object object){

        String type = object.getClass().getSimpleName();
        SharedPreferences sp = MainApplication.getInstance().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        if(SPConstants.PARAMS_ACC_ON_REPORT_TIME.equals(key)){
            //AppLog.d("ltj------------------- BusinessProcessQH","paramValue:"+ object + "      " + AppLog.generateTag(3));
        }
        if("String".equals(type)){
            editor.putString(key, (String)object);
        }
        else if("Integer".equals(type)){
            editor.putInt(key, (Integer)object);
        }
        else if("Boolean".equals(type)){
            editor.putBoolean(key, (Boolean)object);
        }
        else if("Float".equals(type)){
            editor.putFloat(key, (Float)object);
        }
        else if("Long".equals(type)){
            editor.putLong(key, (Long)object);
        }

        editor.commit();
    }





    public synchronized static String getParamString(String key, String str){
        SharedPreferences sp = MainApplication.getInstance().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sp.getString(key, str);
    }
    public synchronized static int getParamInteger(String key, int i){
        SharedPreferences sp = MainApplication.getInstance().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sp.getInt(key, i);
    }
    public synchronized static boolean getParamBoolean(String key, boolean b){
        SharedPreferences sp = MainApplication.getInstance().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sp.getBoolean(key, b);
    }
    public synchronized static float getParamFloat(String key, float f){
        SharedPreferences sp = MainApplication.getInstance().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sp.getFloat(key, f);
    }
    public synchronized static long getParamLong(String key, long l){
        SharedPreferences sp = MainApplication.getInstance().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sp.getLong(key, l);
    }


    /**
     * 清除所有数据
     * @param context
     */
    public static void clearAll(Context context) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear().commit();
    }

    /**
     * 清除指定数据
     * @param context
     */
    public static void clear(Context context, String name) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(name);
        editor.commit();
    }

    /*** 默认存储*/
    private static SharedPreferences defaultPref;
    private static Editor defaultEditor;

    public static SharedPreferences defaultPref(){
        if(defaultPref == null){
            defaultPref = MainApplication.getInstance().getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
        }
        return defaultPref;
    }

    public static Editor defaultEditor(){
        if(defaultEditor == null){
            defaultEditor = defaultPref().edit();
        }
        return defaultEditor;
    }
}